package com.zhiyou.aifeng.smart_machine

import android.os.Bundle
import com.jeremyliao.liveeventbus.LiveEventBus
import com.zhiyou.aifeng.common.base.oneClick
import com.zhiyou.aifeng.common.helper.*
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.base.addItems
import com.zhiyou.aifeng.common_ui.base.setCompoundDrawable
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.model.CommonData
import com.zhiyou.aifeng.common_ui.utils.showListDialog
import com.zhiyou.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_wallet_add.*
import org.jetbrains.anko.toast

class WalletAddActivity : BaseActivity() {

    private var mBankName = ""
    private val mBankList = ArrayList<CommonData>()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wallet_add)
        initTitle(title = "添加银行卡", divider = false)
    }

    override fun getViewModel() = get<WalletViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        wallet_check.setCompoundDrawable(
            leftDrawable = R.drawable.ic_bank_selector,
            leftSize = 17f
        )

        wallet_name.setText(getString("realName"))
        wallet_tel.setText(getString("phone"))

        wallet_place.oneClick {
            getViewModel().requestModel(
                isLoading = true,
                requestUrl = BaseUrl.walletBank,
                onSuccess = { data ->
                    mBankList.clear()
                    mBankList.addItems(data)

                    showListDialog(
                        title = "请选择开户行",
                        items = mBankList.mapTo(ArrayList()) { it.bankname }
                    ) { _, name ->
                        mBankName = name
                        wallet_place.setRightString(mBankName)
                    }
                }
            )
        }

        bt_add.oneClick {
            when {
                wallet_name.text.isBlank() -> {
                    wallet_name.requestFocus()
                    toast("请输入持卡人姓名")
                    return@oneClick
                }
                mBankName.isEmpty() -> {
                    toast("请选择开户行")
                    return@oneClick
                }
                wallet_num.rawText.isNullOrEmpty() -> {
                    wallet_num.requestFocus()
                    toast("请输入银行卡卡号")
                    return@oneClick
                }
                wallet_tel.text.isEmpty() -> {
                    wallet_tel.requestFocus()
                    toast("请输入预留手机号")
                    return@oneClick
                }
                wallet_bank.text.isBlank() -> {
                    wallet_bank.requestFocus()
                    toast("请输入银行卡开户支行")
                    return@oneClick
                }
                !BankcardHelper.checkBankCard(wallet_num.rawText) -> {
                    wallet_num.requestFocus()
                    toast("请输入正确的银行卡卡号")
                    return@oneClick
                }
                !wallet_tel.textString().isMobile() -> {
                    wallet_tel.requestFocus()
                    toast("请输入正确的预留手机号")
                    return@oneClick
                }
            }

            getViewModel().request(
                requestUrl = BaseUrl.walletAdd,
                params = *arrayOf(
                    "userId" to getString("userId"),
                    "name" to wallet_name.trimString(),
                    "phone" to wallet_tel.textString(),
                    "bankname" to mBankName,
                    "cardnumber" to (wallet_num.rawText ?: ""),
                    "bankbranch" to wallet_bank.trimString(),
                    "defaultbank" to (if (wallet_check.isChecked) 1 else 0)
                ),
                onResult = {
                    toast("添加银行卡成功！")
                    LiveEventBus.get("cardList").post("refresh")
                    finishView()
                }
            )
        }
    }

}